package com.bottomlord.bus;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.bus.BusBridge;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ChenYue
 * @date 2021/6/9 21:45
 */
@Component
public class RedisBusBridge implements BusBridge {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private List<String> channels;

    @Override
    public void send(RemoteApplicationEvent event) {
        channels.forEach(channel -> redisTemplate.convertAndSend(channel, MessageBuilder.withPayload(event).build()));
    }
}
